package jmine.tec.utils.io;

import java.net.URL;

/**
 * ResourceFinder
 * 
 * @author takeshi
 */
public final class ResourceFinder {

    /**
     */
    private ResourceFinder() {
    }

    /**
     * Procura um resource, primeiro no contextClassLoader e depois no classloader da classe passada
     * 
     * @param loadingClass {@link Class}
     * @param path String
     * @return {@link URL}
     */
    public static URL findClasspathResourceAbsolutePath(Class<?> loadingClass, String path) {
        URL url = findClasspathResource(Thread.currentThread().getContextClassLoader(), path);
        if (url == null) {
            return findClasspathResource(loadingClass.getClassLoader(), path);
        }
        return url;
    }

    /**
     * Procura um resource
     * 
     * @param loader {@link ClassLoader}
     * @param path String
     * @return {@link URL} ou <code>null</code>
     */
    public static URL findClasspathResource(ClassLoader loader, String path) {
        if (loader == null) {
            return null;
        }
        return loader.getResource(path);
    }

}
